const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
/**
 * @type {import('webpack').Configuration}
 */
class MyPlugin {
  apply(compiler) {
    compiler.hooks.emit.tap('MyPlugin', compilation => {
      // compilation =>可以理解为此次打包的上下文
      for (const name in compilation.assets) {
        if (name.endsWith('.js')) {
          const source = compilation.assets[name].source();
          const withoutComments = source.replace(/\/\*+\//g, '');
          // console.log(withoutComments);
          compilation.assets[name] = {
            source: () => withoutComments,
            size: () => withoutComments.length,
          };
        }
      }
    });
  }
}
module.exports = {
  mode: 'none',
  entry: './src/main.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    // publicPath: 'dist/',
  },
  devtool: '#source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      //  {
      //   test: /\.(jpg|png)$/,
      //   use: ['file-loader']
      //  }
      // 最佳实践：小文件使用Data URLs，减少请求次数
      // 大文件单独提取存放，提高加载速度
      // 如果文件大于10KB，将会自动使用file-loader抽离文件；否则，由url-loader转换为Data URL
      {
        test: /\.(jpg|png)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024 * 10,
            },
          },
        ],
      },
      {
        test: /\.tpl$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              attributes: {
                list: [
                  {
                    tag: 'img',
                    attribute: 'src',
                    type: 'src',
                  },
                  // html-loader默认对img的src引用资源会做处理，通过如下配置，也可以对a标签的href做加载处理
                  {
                    tag: 'a',
                    attribute: 'href',
                    type: 'src',
                  },
                ],
              },
            },
          },
        ],
      },
      {
        test: /\.md$/,
        use: ['html-loader', './markdown-loader'],
      },
    ],
  },
  devServer: {
    port: 3001,
    open: true,
    // 可以访问public下面的文件
    contentBase: './public',
    hot: true,
    proxy: {
      '/api': {
        // http://localhost/api/users => https://api.github.com/api/users
        target: 'https://api.github.com',
        pathRewrite: {
          '^/api': '',
        },
        // 不能使用localhost:8080作为请求GitHub的主机名
        changeOrigin: true,
      },
    },
  },
  plugins: [
    // new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: 'Webpack plugin sample',
      template: path.join(__dirname, 'index.html'),
      meta: {
        viewport: 'width=device-width',
      },
    }),
    // new CopyWebpackPlugin({
    //   patterns: [{ from: 'public', to: '.' }],
    // }),
  ],
};
